package com.droidlogic.mboxlauncher;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.storage.StorageManager;
//import android.os.storage.VolumeInfo;
import android.text.format.DateFormat;
import android.util.ArrayMap;
import android.util.Log;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

public class StatusLoader {
    private final String SDCARD_FILE_NAME = "sdcard";
    private final String STORAGE_PATH = "/storage";
    private final String UDISK_FILE_NAME = "udisk";
    List<ArrayMap<String, Object>> list;
    private ConnectivityManager mConnectivityManager;
    private Context mContext;
    private StorageManager mStorageManager;
    private WifiManager mWifiManager;

    public StatusLoader(Context context) {
        this.mContext = context;
        this.mConnectivityManager = (ConnectivityManager) this.mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
        this.mWifiManager = (WifiManager) this.mContext.getSystemService(Context.WIFI_SERVICE);
        this.mStorageManager = (StorageManager) this.mContext.getSystemService(Context.STORAGE_SERVICE);
    }

    public List<ArrayMap<String, Object>> getStatusData() {
        this.list = new ArrayList();
        ArrayMap<String, Object> map = new ArrayMap<>();
        int wifi_level = getWifiLevel();
        if (wifi_level != -1) {
            switch (wifi_level + 1) {
                case 1:
                    map.put("item_icon", Integer.valueOf(R.drawable.wifi2));
                    break;
                case 2:
                    map.put("item_icon", Integer.valueOf(R.drawable.wifi3));
                    break;
                case 3:
                    map.put("item_icon", Integer.valueOf(R.drawable.wifi4));
                    break;
                case 4:
                    map.put("item_icon", Integer.valueOf(R.drawable.wifi5));
                    break;
                default:
                    map.put("item_icon", Integer.valueOf(R.drawable.wifi1));
                    break;
            }
            this.list.add(map);
        }
        if (isSdcardExist()) {
            ArrayMap<String, Object> map2 = new ArrayMap<>();
            map2.put("item_icon", Integer.valueOf(R.drawable.img_status_sdcard));
            this.list.add(map2);
        }
        if (isUdiskExist()) {
            ArrayMap<String, Object> map3 = new ArrayMap<>();
            map3.put("item_icon", Integer.valueOf(R.drawable.img_status_usb));
            this.list.add(map3);
        }
        if (isEthernetOn()) {
            ArrayMap<String, Object> map4 = new ArrayMap<>();
            map4.put("item_icon", Integer.valueOf(R.drawable.img_status_ethernet));
            this.list.add(map4);
        }
        return this.list;
    }

    private boolean isSdcardExist() {
//        List<VolumeInfo> volumes = this.mStorageManager.getVolumes();
//        Collections.sort(volumes, VolumeInfo.getDescriptionComparator());
//        for (VolumeInfo vol : volumes) {
//            if (vol != null && vol.isMountedReadable() && vol.getType() == 0 && vol.getDisk().isSd()) {
//                return true;
//            }
//        }
//        return false;
        return MOUNTED == sd_mount_state(mContext);
    }

    private boolean isUdiskExist() {
//        List<VolumeInfo> volumes = this.mStorageManager.getVolumes();
//        Collections.sort(volumes, VolumeInfo.getDescriptionComparator());
//        for (VolumeInfo vol : volumes) {
//            if (vol != null && vol.isMountedReadable() && vol.getType() == 0 && vol.getDisk().isUsb()) {
//                return true;
//            }
//        }
//        return false;
        return MOUNTED == usb_mount_state(mContext);
    }

    private int getWifiLevel() {
        if (this.mConnectivityManager.getNetworkInfo(1).isConnected()) {
            return WifiManager.calculateSignalLevel(this.mWifiManager.getConnectionInfo().getRssi(), 4);
        }
        return -1;
    }

    private boolean isEthernetOn() {
        NetworkInfo info = this.mConnectivityManager.getNetworkInfo(9);
        if (info == null || !info.isConnected()) {
            return false;
        }
        return true;
    }

    public String getTime() {
        String time;
        Calendar c = Calendar.getInstance();
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);
        if (!DateFormat.is24HourFormat(this.mContext) && hour > 12) {
            hour -= 12;
        }
        if (hour >= 10) {
            time = "" + Integer.toString(hour);
        } else {
            time = "" + "0" + Integer.toString(hour);
        }
        String time2 = time + ":";
        if (minute >= 10) {
            return time2 + Integer.toString(minute);
        }
        return time2 + "0" + Integer.toString(minute);
    }

    public String getDate() {
        Calendar c = Calendar.getInstance();
        int int_Month = c.get(Calendar.MONTH);
        String mDay = Integer.toString(c.get(Calendar.DAY_OF_MONTH));
        String str_week = this.mContext.getResources().getStringArray(R.array.week)[c.get(Calendar.DAY_OF_WEEK) - 1];
        String mMonth = this.mContext.getResources().getStringArray(R.array.month)[int_Month];
        if (Locale.getDefault().getLanguage().equals("zh")) {
            return str_week + ", " + mMonth + " " + mDay + this.mContext.getResources().getString(R.string.str_day);
        }
        return str_week + ", " + mMonth + " " + mDay;
    }
    public static final int MOUNTED = 1;
    public static final int UNMOUNTED = 0;

    public int sd_mount_state(Context context){
        StorageManager mStorageManager = (StorageManager) context
                .getSystemService(Context.STORAGE_SERVICE);

        Class<?> diskInfoClazz = null;
        try {
            diskInfoClazz = Class.forName("android.os.storage.DiskInfo");

            Method getDisks = mStorageManager.getClass().getMethod("getDisks");

            Method isSd = diskInfoClazz.getMethod("isSd");

            List result = (List) getDisks.invoke(mStorageManager);

            for (int i = 0; i < result.size(); i++) {
                Object diskInfo = result.get(i);
                if ((Boolean) isSd.invoke(diskInfo)){
                    Log.e("tv_storage","SD_MOUNTED");
                    return MOUNTED;
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return UNMOUNTED;
    }

    public int usb_mount_state(Context context){
        StorageManager mStorageManager = (StorageManager) context
                .getSystemService(Context.STORAGE_SERVICE);

        Class<?> diskInfoClazz = null;
        try {
            diskInfoClazz = Class.forName("android.os.storage.DiskInfo");

            Method getDisks = mStorageManager.getClass().getMethod("getDisks");

            Method isUsb = diskInfoClazz.getMethod("isUsb");

            List result = (List) getDisks.invoke(mStorageManager);

            for (int i = 0; i < result.size(); i++) {
                Object diskInfo = result.get(i);
                if ((Boolean) isUsb.invoke(diskInfo)){
                    Log.e("tv_storage","USB_MOUNTED");
                    return MOUNTED;
                }
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return UNMOUNTED;
    }
}